package com.fan;


import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.InputStream;
import java.util.Random;

import static com.fan.EnglishWordsGameMain.TOTAL;
import static com.fan.MyJFrame.wordsArrayList;
import static com.fan.MyJPanel.LOTUS_LEAF_LIST;

/**
 * @author Administrator
 */
public class LotusLeaf extends JPanel {

    private static Image image = null;
    static {
        try{

            InputStream inputStream = MyJPanel.class.getClassLoader().getResourceAsStream("image/LotusLeaf.png");
            image = ImageIO.read(inputStream);

        }catch (Exception e){
            e.printStackTrace();
        }
    }
    private Words words;
    Random random = new Random();

    private boolean isLife;


    public LotusLeaf(Words words) {
        this.setLayout(null);
        this.words = words;
        this.isLife = true;
        this.setBackground(new Color(0,0,0));
        this.setSize(160,30);
        this.setLocation(EnglishWordsGameMain.SCREEN_WIDTH,30+random.nextInt(EnglishWordsGameMain.SCREEN_HEIGHT-150));


        MouseAdapter l = new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                Object source = e.getSource();
                if(source instanceof LotusLeaf){
                    LotusLeaf lotusLeaf =  (LotusLeaf)source;
                    System.out.println(lotusLeaf.getWords().getId());
                    if(lotusLeaf.getWords().getId() == wordsArrayList.get(EnglishWordsGameMain.index).getId()){
                        lotusLeaf.setLife(false);
                        MyJPanel panel = (MyJPanel)lotusLeaf.getParent();
                        MyJPanel.map.remove(lotusLeaf.getWords().getId());
                        System.out.println(lotusLeaf.getWords().getId() + " = "+ EnglishWordsGameMain.index);
                        EnglishWordsGameMain.index = random.nextInt(TOTAL);
                        System.out.println("index   = "+ EnglishWordsGameMain.index);
                        panel.setWords(wordsArrayList.get(EnglishWordsGameMain.index));
                        MyJPanel.SCORE += 10;
                    }else{
                        MyJPanel.SCORE -= 5;
                    }
               }
            }
        };
        this.addMouseListener(l);
        this.addMouseMotionListener(l);
        repaint();

    }

    @Override
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        int width = (int)this.getSize().getWidth()-20;
        int height = (int)this.getSize().getHeight()-10;
//        g.drawImage(image,
//                width-image.getWidth(this),
//                height-image.getHeight(this),
//                width,
//                height,
//                this);
        Font f = new Font("微软雅黑",Font.BOLD,20);
        g.setFont(f);
        g.setColor(new Color(255,90,0));
        g.drawString(this.getWords().getEnglish(), width-100,height);

    }

    public Words getWords() {
        return words;
    }

    public void setWords(Words words) {
        this.words = words;
    }


    public Image getImage() {
        return image;
    }

    public boolean isLife() {
        return isLife;
    }

    public void setLife(boolean life) {
        isLife = life;
    }
}
















